import 'package:flutter/material.dart';
import '../models/theory_model.dart';

class TheoryEditPage extends StatefulWidget {
  final TheoryContent? content;
  final void Function(TheoryContent) onSave;
  const TheoryEditPage({Key? key, this.content, required this.onSave}) : super(key: key);

  @override
  State<TheoryEditPage> createState() => _TheoryEditPageState();
}

class _TheoryEditPageState extends State<TheoryEditPage> {
  final _formKey = GlobalKey<FormState>();
  late String jing;
  late String summary;
  late String pathogenesis;
  late String symptoms;
  late String therapy;
  late String formula;
  bool isFavorite = false;
  bool hasNote = false;
  bool isKey = false;

  @override
  void initState() {
    super.initState();
    final c = widget.content;
    jing = c?.jing ?? '';
    summary = c?.summary ?? '';
    pathogenesis = c?.pathogenesis ?? '';
    symptoms = c?.symptoms ?? '';
    therapy = c?.therapy ?? '';
    formula = c?.formula ?? '';
    isFavorite = c?.isFavorite ?? false;
    hasNote = c?.hasNote ?? false;
    isKey = c?.isKey ?? false;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.content == null ? '添加理论内容' : '编辑理论内容'),
        backgroundColor: Colors.brown[50],
        elevation: 0,
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: ListView(
            children: [
              TextFormField(
                initialValue: jing,
                decoration: const InputDecoration(labelText: '六经名'),
                validator: (v) => v == null || v.isEmpty ? '请输入六经名' : null,
                onSaved: (v) => jing = v ?? '',
              ),
              TextFormField(
                initialValue: summary,
                decoration: const InputDecoration(labelText: '概述'),
                onSaved: (v) => summary = v ?? '',
              ),
              TextFormField(
                initialValue: pathogenesis,
                decoration: const InputDecoration(labelText: '病机'),
                onSaved: (v) => pathogenesis = v ?? '',
              ),
              TextFormField(
                initialValue: symptoms,
                decoration: const InputDecoration(labelText: '症状'),
                onSaved: (v) => symptoms = v ?? '',
              ),
              TextFormField(
                initialValue: therapy,
                decoration: const InputDecoration(labelText: '治法'),
                onSaved: (v) => therapy = v ?? '',
              ),
              TextFormField(
                initialValue: formula,
                decoration: const InputDecoration(labelText: '方剂'),
                onSaved: (v) => formula = v ?? '',
              ),
              SwitchListTile(
                title: const Text('收藏'),
                value: isFavorite,
                onChanged: (v) => setState(() => isFavorite = v),
              ),
              SwitchListTile(
                title: const Text('有笔记'),
                value: hasNote,
                onChanged: (v) => setState(() => hasNote = v),
              ),
              SwitchListTile(
                title: const Text('重点'),
                value: isKey,
                onChanged: (v) => setState(() => isKey = v),
              ),
              const SizedBox(height: 24),
              ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.brown,
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
                ),
                onPressed: () {
                  if (_formKey.currentState?.validate() ?? false) {
                    _formKey.currentState?.save();
                    widget.onSave(TheoryContent(
                      id: widget.content?.id ?? UniqueKey().toString(),
                      jing: jing,
                      summary: summary,
                      pathogenesis: pathogenesis,
                      symptoms: symptoms,
                      therapy: therapy,
                      formula: formula,
                      isFavorite: isFavorite,
                      hasNote: hasNote,
                      isKey: isKey,
                    ));
                    Navigator.of(context).pop();
                  }
                },
                child: Text(widget.content == null ? '添加' : '保存', style: const TextStyle(color: Colors.black)),
              ),
            ],
          ),
        ),
      ),
    );
  }
} 